package com.example.enjoylearning.login

import android.content.Context
import com.example.enjoylearning.api.WanAndroidApi
import com.example.enjoylearning.model.LoginBen
import com.example.enjoylearning.network.ApiClient
import com.example.enjoylearning.network.ApiError
import com.example.enjoylearning.network.ApiResponse
import com.example.enjoylearning.network.NetworkScheduler

/**
 * @Author ninetailedfox
 * @Data 2022/10/31 10:19 下午
 * @ClassName LoginModuleImpl
 **/
class LoginModuleImpl : LoginModel {

    override fun login(
        context: Context,
        username: String,
        password: String,
        onLoginListener: LoginPresenter.OnLoginListener
    ) {
        ApiClient.instance.getService(WanAndroidApi::class.java)
            .loginWanAndroid(username, password)
            .compose(NetworkScheduler.compose())
            .subscribe(
                object : ApiResponse<LoginBen>(context) {
                    override fun success(data: LoginBen) {
                        onLoginListener.loginSuccess(data)
                    }

                    override fun failure(statusCode: Int, apiError: ApiError) {
                        onLoginListener.loginFailed(apiError.message)
                    }

                }
            )
    }

    override fun register(
        context: Context,
        username: String,
        password: String,
        repassword: String,
        onRegisterListener: LoginPresenter.OnRegisterListener
    ) {
        ApiClient.instance.getService(WanAndroidApi::class.java)
            .registerWanAndroid(username, password, repassword)
            .compose(NetworkScheduler.compose())
            .subscribe(
                object : ApiResponse<LoginBen>(context) {
                    override fun success(data: LoginBen) {
                        if (data!=null){
                            onRegisterListener.registerSuccess(data)
                        }

                    }

                    override fun failure(statusCode: Int, apiError: ApiError) {
                        onRegisterListener.registerFailed(apiError.message)
                    }

                }
            )
    }

}